package cn.itcast.web.controller.basicInformation;

import cn.itcast.domain.cargo.Factory;
import cn.itcast.domain.cargo.FactoryExample;
import cn.itcast.service.cargo.FactoryInformationService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;

@Controller
@RequestMapping("/basicInformation/factoryInformation")
public class FactoryInformationController extends BaseController {


    @Reference
    private FactoryInformationService factoryInformationService;


     /*url:  /basicInformation/factoryInformation/list.do
     参数： 无
     作用： 进入厂家的列表页面
     返回值: 厂家的列表页面*/



    @RequestMapping("/list")
    public  String list(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "5") int pageSize){

        FactoryExample factoryExample = new FactoryExample();
        factoryExample.createCriteria();

        PageInfo<Factory> pageInfo = factoryInformationService.findByPage(factoryExample, pageNum, pageSize);
        //存储到域对象
        request.setAttribute("pageInfo",pageInfo);
        //返回到用户分页列表
        return "basicInformation/factoryInformation/factory-list";
    }


    /*url:/basicInformation/factoryInformation/toAdd.do
    作用： 进入添加页面
    参数： 无
    */

    @RequestMapping("/toAdd")
    public  String toAdd(){
        return "basicInformation/factoryInformation/factory-add";
    }

        /*url:/basicInformation/factoryInformation/toUpdate.do?id="+id,
        作用： 进入添加页面
        参数： 无*/


    @RequestMapping("/toUpdate")
    public String toUpdate(String id){
        //1. 查找厂家
        Factory factory = factoryInformationService.findById(id);
        request.setAttribute("factory",factory);
        return "basicInformation/factoryInformation/factory-update";
    }


     /*    url:/basicInformation/factoryInformation/toUpdate.do?id="+id,
        作用： 删除厂家
        参数： 厂家的id
        返回值：厂家列表页面*/


    @RequestMapping("/delete")
    public String delete(String id){
        factoryInformationService.delete(id);
        return "redirect:/basicInformation/factoryInformation/list.do";
    }


         /* url: /basicInformation/factoryInformation/edit.do
          作用： 新增厂家||更新厂家
          参数： 厂家
          返回值：厂家列表页面*/


    @RequestMapping("/edit")
    public String edit(Factory factory){

        //补全厂家信息
        factory.setCompanyId(getLoginCompanyId());
        factory.setCompanyName(getLoginCompanyName());

        //if(factory.getId()==null||factory.getId().equals("")){
        if(StringUtils.isEmpty(factory.getId())){
            //没有带id过来 , 新增操作
            //补全厂家创建人与创建人所属的部门
            factory.setCreateBy(getLoginUser().getId());  //登陆者的id
            factory.setCreateDept(getLoginUser().getDeptId());  //登陆者所属的部门
            factory.setCreateTime(new Date());
            factory.setUpdateBy(getLoginUser().getId());
            factory.setUpdateTime(new Date());

            factoryInformationService.save(factory);
        }else{
            //更新   update xx set xx=xx  xx=xx where id = xx  更新是一定要带着id过来

            factory.setUpdateBy(getLoginUser().getId());
            factory.setUpdateTime(new Date());

            factoryInformationService.update(factory);
        }
        //更新或者新增成功都要回到厂家列表页面
        return "redirect:/basicInformation/factoryInformation/list.do";  //添加redirect开头的返回值则不会经过视图解析器
    }


}
